﻿
var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        postStr: '',
        form: {
            ID: 0,
            Name: '',
            TotalCost: '',
            DiscountID: 0,
            DiscountCost: '',
            BreakTotalCost: '',
            ChargeID: '',
            StartTime: '',
            EndTime: '',
            UseCount: '',
            RoomID: 0,
            UnitPrice: 0,
            CalMonthNumber: 0,
            AutoToMonthEnd: 0,
            CanChangeEndTime: false
        },
        manualEndTime: 0
    },
    methods: {
        do_open_discount: function () {
            var that = this;
            ns.openWin('chargeroomfee_discount_request_frm', '优惠金额调整', {
                id: that.form.ID,
                chargeId: that.form.ChargeID,
                feeStartTime: that.form.StartTime,
                feeEndTime: that.form.EndTime,
                totalCost: that.form.TotalCost,
                useCount: that.form.UseCount,
                roomId: that.form.RoomID,
                unitPrice: that.form.UnitPrice,
                breakTotalCost: that.form.BreakTotalCost,
                discountId: that.form.DiscountID
            });
        },
        //减少月数
        do_reduce_month: function () {
            var that = this;
            if (!that.form.IsCanSelect) {
                return;
            }
            if (!that.form.NeedChargeWechatAll) {
                that.form.Selected = true;
            }
            if (that.form.MonthAddCount > that.form.MinMonthAddNumber) {
                that.form.MonthAddCount--;
            }
            that.form.CalMonthNumber = that.form.MonthAddNumber * that.form.MonthAddCount;
            that.get_roomfee_bytime(that.form.CalMonthNumber);
        },
        //增加月数
        do_add_month: function () {
            var that = this;
            if (!that.form.IsCanSelect) {
                return;
            }
            if (!that.form.NeedChargeWechatAll) {
                that.form.Selected = true;
            }
            if (that.form.MaxMonthAddNumber > 0 && that.form.CalMonthNumber >= that.form.MaxMonthAddNumber) {
                that.form.CalMonthNumber = that.form.MaxMonthAddNumber;
            } else {
                that.form.MonthAddCount++;
                that.form.CalMonthNumber = that.form.MonthAddNumber * that.form.MonthAddCount;
                if (that.form.MaxMonthAddNumber > 0 && that.form.CalMonthNumber >= that.form.MaxMonthAddNumber) {
                    that.form.CalMonthNumber = that.form.MaxMonthAddNumber;
                }
            }
            that.get_roomfee_bytime(that.form.CalMonthNumber);
        },
        do_save: function () {
            var that = this;
            api.sendEvent({
                name: 'saveroomfeefielddone',
                extra: that.form
            });
            api.closeWin();
        },
        get_roomfee_bytime: function (CalMonthNumber) {
            var that = this;
            ns.get({
                action: 'getroomfeebyid',
                id: that.form.ID,
                endtime: that.form.EndTime,
                CalMonthNumber: CalMonthNumber,
                ChargeID: that.form.ChargeID,
                StartTime: that.form.StartTime,
                AutoToMonthEnd: that.form.AutoToMonthEnd ? 1 : 0,
                manualEndTime: that.manualEndTime
            }, function (succeed, data, err) {
                that.manualEndTime = 0;
                if (succeed) {
                    that.form.TotalCost = data.TotalCost;
                    that.form.BreakTotalCost = data.BreakTotalCost;
                    that.form.EndTime = data.EndTime;
                    that.form.CalMonthNumber = data.CalMonthNumber;
                    that.form.DiscountID = data.discountId;
                    that.form.DiscountCost = data.discountCost;
                    if (that.form.MonthAddNumber > 0) {
                        that.form.MonthAddCount = parseInt(that.form.CalMonthNumber / that.form.MonthAddNumber);
                    }
                    var totalFee = 0;
                    if (data.BreakTotalCost > 0) {
                        totalFee += data.BreakTotalCost;
                    }
                }
            });
        },
        do_select_time: function () {
            var that = this;
            if (!that.form.IsCanSelect) {
                return;
            }
            if (!that.form.CanChangeEndTime) {
                return;
            }
            api.openPicker({
                type: 'date',
                date: that.form.EndTime,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    that.manualEndTime = 1;
                    that.form.EndTime = year + '-' + month + '-' + day;
                    that.get_roomfee_bytime(0);
                }
            });
        }
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.postStr = api.pageParam.postStr || '';
    app.form = eval('(' + app.postStr + ')')
    if (app.form.MonthAddNumber > 0) {
        app.form.MonthAddCount = parseInt(app.form.CalMonthNumber / app.form.MonthAddNumber);
    }
    api.addEventListener({
        name: 'submitdiscountsuccess'
    }, function (ret) {
        app.form.id = ret.value.feeId;
        app.form.DiscountCost = Number(ret.value.cost);
        app.form.DiscountID = ret.value.discountId;
        app.form.DiscountApproveState = ret.value.stateId;
        app.form.DiscountApproveStateDesc = ret.value.stateDesc;
    })
};
